;$(function () {
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table;
        table.render({
            elem: "#saleWaresInfoTable"
            , url: "saleWaresInfoSearch"
            , method: "post"
            , response: {
                statusCode: true
            }
            , defaultToolbar: []
            , cellMinWidth: 70
            , cols: [[
                {field: 'sorts', title: '序号', width: 60, templet: '#sort', align: 'center'}
                , {field: 'waresId', title: '商品编号', align: 'center', sort: true}
                , {field: 'waresName', title: '商品名称', align: 'center'}
                , {field: 'categoryInfo', title: '分类', align: 'center', width: 150}
                , {field: 'promotionId', title: '活动编号', align: 'center'}
                , {field: 'promotionName', title: '活动名称', align: 'center'}
                , {field: 'promotionType', title: '活动类型', align: 'center'}
                , {field: 'promotionStartDate', title: '开始时间', sort: true, align: 'center'}
                , {field: 'promotionEndDate', title: '结束时间', sort: true, align: 'center'}
                , {
                    field: 'tool', title: '操作', align: 'center',
                    templet: function (d) {
                        return '<span style="text-decoration:underline;cursor:pointer;color:#D92E2E" lay-event="delSaleWares">移除商品</span>'
                    }
                }
            ]]
            , page: true
            , id: "saleWaresInfoTableRender"
        });
        $("#saleWaresSearch").click(function () {
            saleWaresInfoTableRenderFunction();
        });
        $("#salePromotionSearch").click(function () {
            saleWaresInfoTableRenderFunction();
        });
        form.on('select(salePromotionType)', function () {
            saleWaresInfoTableRenderFunction();
        });

        function saleWaresInfoTableRenderFunction() {
            let wValue = $("#saleWaresSearch").prev().val();
            let pValue = $("#salePromotionSearch").prev().val();
            let ptValue = $("#salePromotionType").val();
            table.reload('saleWaresInfoTableRender', {
                where: { //设定异步数据接口的额外参数，任意设
                    waresInfo: wValue
                    , promotionInfo: pValue
                    , promotionType: ptValue
                }
                , page: {
                    curr: 1 //重新从第// 1 页开始
                }
            });
        }

        table.on("tool(saleWaresInfoTable)", function (obj) {
            switch (obj.event) {
                case  'delSaleWares':
                    layer.confirm('确定移除？', function (index) {
                        layer.close(index);
                        $.ajax({
                            url: 'delSaleWares',
                            type: "post",
                            dataType: "json",
                            data: {
                                "promotionId": obj.data.promotionId,
                                "waresId": obj.data.waresId
                            },
                            // async: false,
                            success: function (result) {
                                if (result.code === true) {
                                    table.reload('saleWaresInfoTableRender', function () {
                                        page: {
                                            curr: 1 //重新从第// 1 页开始
                                        }
                                    });
                                }
                                layer.msg(result.msg);
                            },
                        });
                    });
                    break;
            }
        });
    });
});